// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.core.logger;

import org.lwjglx.opengl.OpenGLException;
import zombie.core.Core;
import zombie.core.SpriteRenderer;
import zombie.core.opengl.RenderThread;
import zombie.debug.DebugLog;
import zombie.debug.DebugLogStream;
import zombie.debug.LogSeverity;
import zombie.network.GameServer;
import zombie.ui.TextManager;
import zombie.ui.UIFont;
import zombie.ui.UIManager;
import zombie.ui.UITransition;
import zombie.util.Type;

public final class ExceptionLogger {
    private static int exceptionCount;
    private static boolean bIgnore;
    private static boolean bExceptionPopup = true;
    private static long popupFrameMS = 0L;
    private static UITransition transition = new UITransition();
    private static boolean bHide;

    public static synchronized void logException(Throwable ex) {
        logException(ex, null);
    }

    public static synchronized void logException(Throwable ex, String errorMessage) {
        logException(ex, errorMessage, DebugLog.General, LogSeverity.Error);
    }

    public static synchronized void logException(Throwable ex, String errorMessage, DebugLogStream out, LogSeverity severity) {
        OpenGLException openGLException = Type.tryCastTo(ex, OpenGLException.class);
        if (openGLException != null) {
            RenderThread.logGLException(openGLException, false);
        }

        out.printException(ex, errorMessage, DebugLogStream.generateCallerPrefix(), severity);

        try {
            if (bIgnore) {
                return;
            }

            bIgnore = true;
            exceptionCount++;
            if (!GameServer.bServer) {
                if (bExceptionPopup) {
                    showPopup();
                }

                return;
            }
        } catch (Throwable throwable) {
            out.printException(throwable, "Exception thrown while trying to logException.", LogSeverity.Error);
            return;
        } finally {
            bIgnore = false;
        }
    }

    public static void showPopup() {
        float _float = popupFrameMS > 0L ? transition.getElapsed() : 0.0F;
        popupFrameMS = 3000L;
        transition.setIgnoreUpdateTime(true);
        transition.init(500.0F, false);
        transition.setElapsed(_float);
        bHide = false;
    }

    public static void render() {
        if (!UIManager.useUIFBO || Core.getInstance().UIRenderThisFrame) {
            boolean _boolean = false;
            if (_boolean) {
                popupFrameMS = 3000L;
            }

            if (popupFrameMS > 0L) {
                popupFrameMS = (long)((double)popupFrameMS - UIManager.getMillisSinceLastRender());
                transition.update();
                int int0 = TextManager.instance.getFontHeight(UIFont.DebugConsole);
                byte _byte = 100;
                int int1 = int0 * 2 + 4;
                int int2 = Core.getInstance().getScreenWidth() - _byte;
                int int3 = Core.getInstance().getScreenHeight() - (int)((float)int1 * transition.fraction());
                if (_boolean) {
                    int3 = Core.getInstance().getScreenHeight() - int1;
                }

                SpriteRenderer.instance.renderi(null, int2, int3, _byte, int1, 0.8F, 0.0F, 0.0F, 1.0F, null);
                SpriteRenderer.instance.renderi(null, int2 + 1, int3 + 1, _byte - 2, int0 - 1, 0.0F, 0.0F, 0.0F, 1.0F, null);
                TextManager.instance.DrawStringCentre(UIFont.DebugConsole, (double)(int2 + _byte / 2), (double)int3, "ERROR", 1.0, 0.0, 0.0, 1.0);
                TextManager.instance
                    .DrawStringCentre(
                        UIFont.DebugConsole,
                        (double)(int2 + _byte / 2),
                        (double)(int3 + int0),
                        _boolean ? "999" : Integer.toString(exceptionCount),
                        0.0,
                        0.0,
                        0.0,
                        1.0
                    );
                if (popupFrameMS <= 0L && !bHide) {
                    popupFrameMS = 500L;
                    transition.init(500.0F, true);
                    bHide = true;
                }
            }
        }
    }
}
